package com.ithei.date.异常;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:  ");
        String name = sc.next();
        Student1 stu = new Student1();
        stu.setName(name);
        //int age=Integer.parseInt(agee) ;
        while (true) {
            try {
                System.out.println("请输入年龄:   ");
                int age = Integer.parseInt(sc.next());
                stu.setAge(age);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入一个整数...");
                //continue;
            } catch (RuntimeException e) {
                System.out.println("请输入一个符合范围的年龄...........");
                e.printStackTrace();
                //continue;
            }

        }


    }
}

class Student1 {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public Student1() {
    }

    public void setAge(int age) {
        if (age >= 18 && age <= 25) {
            this.age = age;
        } else {
            throw new RuntimeException("年龄不在范围...");
        }
    }

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private String name;
    private int age;

}
